package com.kim.pong.producer;

import com.kim.pong.service.PongService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class Producer {
    private Logger logger = LoggerFactory.getLogger(Producer.class);


    @Scheduled(fixedRate = 1000)
    public void sendMessage() throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("producer1");
        producer.setNamesrvAddr("127.0.0.1:9876");
        producer.start();

        // set 2 message in MQ every second
        Message message1 = new Message("Rate1","Tag1",new Date().toString().getBytes());
        SendResult sendResult1 = producer.send(message1);
        logger.info("send message1 MQ :" + sendResult1.getSendStatus());

        Message message2 = new Message("Rate1","Tag1",new Date().toString().getBytes());
        SendResult sendResult2 = producer.send(message2);
        logger.info("send message1 MQ :" + sendResult2.getSendStatus());


        producer.shutdown();

    }


}
